function TakeExam() {
    var _this = this;
    var page;
    _this.page_size = 10;

    // init
    _this.init = function(_page) {
        page = _page;
        _this.initTable();
        _this.initEvent();
    };

    _this.initTable = function() {
        var list_url = appPath + "/admin/projectTakeExam/projectList";
        page.init("takeExamForm", list_url, "pro_tbody", "pro_page", 1, _this.page_size);
        page.goPage(1);
        page.list = function(dataList){
            _this.obj = dataList;
            if(dataList == null){
                return;
            }
            var len = dataList.length;
            var inner = "", item;
            // 组装数据
            for(var i=0; i< len; i++) {
                item = dataList[i];
                // 组装数据
                inner += "<tr>";
                inner += "<td><input type=\"checkbox\" name=\"proBox\" value=\"" + item.id +"\" template=\"";
                if(item.hasOwnProperty('estimateReport')){
                    inner += "template";
                }
                inner += "\"></td>";
                inner += "<td><span class=\"text-orange tooltip\" data-length='30'>"+item.project_name+"</span></td>";
                inner += "<td>"+(TimeUtil.longMsTimeConvertToDateTime(item['project_start_time'])+"<br>"+TimeUtil.longMsTimeConvertToDateTime(item['project_end_time']))+"</td>";
                inner += "<td>"+item.person_count+"</td>";
                inner += "<td>"+(item.is_take_exam == '1' ? "抽考"+item.take_exam_rate.toFixed(1)+"%" : "不限定抽考")+"</td>";
                inner += "<td><span class=\"text-blue\">" +Enum.projectStatus(item.project_status)+ "</span></td>";
                inner += "<td>"+item.create_user+"</td>";
                inner += "<td>"+TimeUtil.longMsTimeConvertToDateTime(item.create_time)+"</td>";
                inner += "<td>";
                if(1 == item.project_status) {
                    inner += '<a  onclick=\"takeExam.project_is_publish(\'' + item.id + '\',\'' + item.project_type + '\',\'' + TimeUtil.longMsTimeToDateTime(item.project_start_time) + '\',\'' + TimeUtil.longMsTimeToDateTime(item.project_end_time) + '\',1);\" class=\"a a-info\">发布</a>';
                }

                if(2 == item.project_status) {
                    inner += '<a  onclick=\"takeExam.project_is_publish(\'' + item.id + '\',\'' + item.project_type + '\',\'' + TimeUtil.longMsTimeToDateTime(item.project_start_time) + '\',\'' + TimeUtil.longMsTimeToDateTime(item.project_end_time) + '\',2);\" class=\"a  a-close\">取消发布</a>';
                }

                inner += '<a href="javascript:takeExam.projectInfo(\''+item.id+'\');" class="a a-view">详情</a> ';

                if(4 != item.project_status && "0"==item.project_mode && item.permission){
                    inner += '<a href="javascript:takeExam.modify(\''+item.id + '\',\''+ item.project_type + '\',\''+ item.project_status  +'\');" class="a a-info">修改</a> ';
                }else{
                    inner +=  '<a href="javascript:;"  class="a">修改</a>';
                }
                if(("1"==item.project_status || "2"==item.project_status && "0"==item.project_mode) && item.permission){
                    inner += '<a href="javascript:takeExam.del(\'' + item.id + '\');" class="a a-close">删除</a>';
                }else{
                    inner += '<a href="javascript:;" class="a">删除</a>';
                }
                // 不限定抽考项目二维码
                if('2' == item.is_take_exam && '1' != item.project_status && '2' != item.project_status){
                    inner += '<a href="javascript:takeExam.qrCode(\'' + item.id + '\',\'' + item.project_status + '\');" class="a a-view">考试二维码</a>';
                }
                // 进行中包含考试的项目 包含主观题
                if(1 == item.readPaper && (3 == item.project_status || 4 == item.project_status) && (3 == item.project_type || 5 == item.project_type || 6 == item.project_type || 7 == item.project_type) && item.permission){
                    inner += '<a href="javascript:readPaper(\''+item.id+'\');" class="a a-view">主观题阅卷</a> ';
                }
                inner += "</td>";
                inner += "</tr>";
            }
            return inner;
        }

    };


    // 考试二维码
    _this.qrCode = function (id, project_status) {
        if('4' == project_status){
            layer.msg("二维码已过期");
            return;
        }else{
            // 考试二维码 弹窗
            layer.open({
                type: 1,
                title: '考试二维码',
                shadeClose: true,
                shade: 0.1,
                area: ['300px', '300px'],
                content: '<div id="qrCodeDiv" style="margin: 25px 50px;"></div>'
            });
            var param = {'project_id':id, 'train_type':'1', 'create_user':''};
            $("#qrCodeDiv").empty();
            //使用jQuery.qrcode.min.js生成二维码。
            $('#qrCodeDiv').qrcode({
                render: "canvas",
                width: 200,
                height: 200,
                text: JSON.stringify(param)//'projectId:'+id,  //二维码的内容
                // correctLevel: QRErrorCorrectLevel.M
            });
        }



    };




    // 项目自定义排序
    _this.sort = function(value, obj) {
        $("#sortBy").val(value);
        var order = obj.className
        if(order == 'sort asc'){
            $("#descAsc").val('DESC')
        }else{
            $("#descAsc").val('ASC')
        }
        // 刷新列表
        _this.initTable(page);
    };

    _this.initEvent = function () {
        //搜索
        $("#project_search").click(function () {
            _this.initTable();
        });

        //全部
        $("#project_all").click(function () {
            $("#projectStartTime").val("");
            $("#projectEndTime").val("");
            $("#projectName").val("");
            _this.initTable();
        });

        //项目类型切换
        $('#click_project_type').on("click", "span", function(){
            $('#click_project_type').find("span").removeClass("active");
            $(this).addClass("active");
            bind_click(this);
        });
    };


    bind_click = function(t){
        var value = $(t).attr("data-value");
        $("#projectType").val(value);
        _this.initTable(page);
    };


    // 打印试卷
    _this.exportPaper = function (projectId) {
        var exam_no = '';
        // var winopn = window.open("", "project_paper");
        // //如果ajax响应时间过长，就给个提示
        // winopn.document.write('<style>body{margin:0;padding:0;background: #e8edf0;overflow: hidden;}</style><img src="' + resourcePath + 'static/global/images/import_paper.png" style="width:100%;height:auto;margin-top:9%;"/>');
        var flag = true;
        while (flag){
            var url = appPath + '/admin/exportProjectPaper';
            var param = {"projectId": projectId, "roleId": "-1"};
            var data = ajax(url, param);
            console.log(data);
            var code = data['code'];
            if (code == 10000) {
                var result = data['result'];
                if (result && result != '') {
                    flag = false;
                    exam_no = result;
                    setCookie('exam_no' + projectId, exam_no);
                    window.location.href = appPath +  "/admin/exportExam?examNo=" + exam_no + "&projectId=" + projectId + "&isCenterTrain=" + "1";
                } else {
                    flag = true;
                }
            } else {
                flag = false;
                layer.alert("网络异常", {icon: 3});
            }
        }
    }



    function ajax(url,param){
        var result = '';
        $.ajax({
            url: url,
            async: false,
            type: 'post',
            data: param,
            success: function(data){
                result = data;
            }
        });
        return result;
    }


    // 项目详情
    _this.projectInfo = function (projectId) {
        var url = appPath + "/admin/project/proManagerInfo?projectId="+projectId+"&type=proManager";
        window.open(url,"_blank");
    };

    // 修改项目
    _this.modify = function(projectId,projectType,projectStatus) {
        location.href = appPath +"/admin/project_create/private/basicInfoModify?projectId="+projectId+"&projectTypeNo="+projectType+"&projectStatus="+projectStatus;
    };

    // 删除项目
    _this.del = function (projectId) {
        layer.confirm("确定删除吗?", {
            icon : 3,
            btn : [ "确认", "取消" ]
        }, function(index) {
            $.ajax({
                url: appPath + '/admin/project/proManagerDelete',
                dataType: 'json',
                async: false,
                type: 'post',
                data: {'projectId': projectId, 'isCenterTrain':2},
                success: function (data) {
                    var result = data.code;
                    if (10000 == result) {
                        layer.msg("删除成功" , {icon : 1,time:500},function (index) {
                            layer.close(index);
                            //刷新列表
                            _this.initTable();
                        });
                    } else {
                        layer.alert(data.message, {icon : 2});
                    }
                }
            });
        });
    };


    // 创建项目
    _this.createTakeExamProject = function () {
        window.location.href = appPath + "/admin/projectTakeExam/createTakeExamProject";
    }



    /*取消或者发布项目*/
    _this.project_is_publish = function (id, projectType, startTime, endTime, createType, projectStatus) {
        var url = "/admin/project/proManagerPublish";
        var param = {
            'id': id,
            'projectType': projectType,
            'startTime': startTime,
            'endTime': endTime,
            'type': createType,
            'isCenterTrain': 0  //0线上项目；1集中培训
        }
        if (createType == 1) {
            layer.confirm("确定要发布该项目吗?", {icon: 3}, function () {
                $.ajax({
                    url: appPath + url,
                    type: "POST",
                    cache: false,
                    async: false,
                    dataType: "json",
                    data: param,
                    success: function (result) {
                        if (result.code == '10000' && result.result == null) {
                            _this.initTable();
                            layer.msg("发布成功", {time: 1000});
                        } else {
                            layer.alert(result.result);
                        }
                    }
                });
            });
        }
        if (createType == 2) {
            layer.confirm("确定要取消发布该项目吗?", {icon: 3}, function () {
                $.ajax({
                    url: appPath + url,
                    type: "POST",
                    cache: false,
                    async: false,
                    dataType: "json",
                    data: param,
                    success: function (result) {
                        if (result.code == '10000') {
                            _this.initTable();
                            layer.msg("取消发布成功", {time: 1000});
                        } else {
                            layer.msg("取消发布失败");
                        }
                    }
                });
            });
        }
        if (createType == 3) {
            $.ajax({
                url: appPath + "/admin/project/proManagerPublish",
                type: "POST",
                cache: false,
                async: false,
                dataType: "json",
                data: {'id': id, 'type': createType, 'projectStatus': projectStatus},
                success: function (result) {

                }
            });
        }
    }

    // 主观题阅卷
    _this.readPaper = function (projectId) {
        // 是否有人 线上正式考试
        $.ajax({
            url: appPath + '/readPaper/projectExamUserCount',
            async: false,
            type: 'post',
            data: {'projectId':projectId, 'examStatus':'2,3'},
            success: function (data) {
                if(data.code != 10000){
                    layer.msg(data.message);
                    return;
                }
                var url = appPath + "/readPaper/index?projectId="+projectId;
                window.open(url,"_blank");
            }
        });
    }


}



function utf16to8(str) {
    var out = "", c, j;
    for(j = 0; j < str.length; j++) {
        c = str.charCodeAt(j);
        if ((c >= 0x0001) && (c <= 0x007F)) {
            out += str.charAt(j);
        } else if (c > 0x07FF) {
            out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
            out += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));
            out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
        } else {
            out += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));
            out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
        }
    }
    return out
}
var takeExam = new TakeExam();